package cn.ylz.ylmall.product.service;

import cn.ylz.ylmall.account.pojo.vo.PageData;
import cn.ylz.ylmall.common.util.PageInfoToPageDataConverter;
import cn.ylz.ylmall.product.dao.persist.mapper.ProductsLikeMapper;
import cn.ylz.ylmall.product.dao.persist.mapper.ProductsMapper;
import cn.ylz.ylmall.product.dao.persist.mapper.ShowCollectProductsMapper;
import cn.ylz.ylmall.product.pojo.entity.Products;
import cn.ylz.ylmall.product.pojo.entity.ProductsLike;
import cn.ylz.ylmall.product.pojo.vo.ShowCollectProductsVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class ProductsService {
    @Autowired
    private ProductsMapper productsMapper;
    @Autowired
    private ProductsLikeMapper productsLikeMapper;
    @Autowired
    private ShowCollectProductsMapper showCollectProductsMapper;
    @Value("${ylmall.dao.default-query-page-size}")
    private Integer pageSize;


    /**
     * 添加
     */
    @Transactional
    public void save(Products productsEntity) {
        log.info("添加商品:" + productsEntity.toString());
        if (productsEntity.getProductId()==0) {
            //新增
            productsMapper.insert(productsEntity);
        } else {
            //修改
            productsMapper.updateById(productsEntity);
        }
    }

    /**
     * 删除
     */
    @Transactional
    public void deleteById(String id) {
        productsMapper.deleteById(id);
    }


    /**
     * 根据ID查询
     *
     */
    public Products getById(String id) {
        Products productsEntity = productsMapper.selectById(id);
        return productsEntity;
    }

    /**
     * 分页查询
     *
     */
    public IPage list(Page page, String query) {
        IPage<Products> iPage = productsMapper.pageList2(page, query);
        log.info("分页查询:" + iPage.toString());
        return iPage;
    }
      /**
       * 通过id集合删除
       */
      @Transactional
      public void deleteByIds(List<String> ids) {
          productsMapper.deleteBatchIds(ids);
      }



      public String collectProducts(Long ProductsId,Long userId){
          QueryWrapper<ProductsLike> queryWrapper = new QueryWrapper<>();
          queryWrapper.eq("product_id",ProductsId)
                  .eq("user_id",userId);
          Integer count = productsLikeMapper.selectCount(queryWrapper);
          ProductsLike productsLike = new ProductsLike();
          productsLike.setUserId(userId);
          productsLike.setProductId(ProductsId);
          String s1;
          if(count >0){
              productsLikeMapper.delete(queryWrapper);
              s1 = "取消收藏成功";
          }
          else{
              productsLike.setCollectTime(LocalDateTime.now());
              productsLikeMapper.insert(productsLike);
              s1 = "收藏成功";
          }
          return s1;
      }

      public boolean likeOrNotProducts(Long ProductsId,Long userId){
          QueryWrapper<ProductsLike> queryWrapper = new QueryWrapper<>();
          queryWrapper.eq("product_id",ProductsId)
                  .eq("user_id",userId);
          Integer count = productsLikeMapper.selectCount(queryWrapper);
          boolean s1;
          if(count >0)
              s1 = true;
          else
              s1 = false;
          return s1;
      }


    public PageData showCollectProducts(Integer pageNum, Long userId) {
        PageHelper.startPage(pageNum,pageSize);
        List<ShowCollectProductsVO> list1 = showCollectProductsMapper.selectById(userId);

        for(ShowCollectProductsVO vo : list1){
            String[] newAddresses = vo.getImageUrls().toString().split(",");
            for(int i = 0; i < newAddresses.length; i++) {
                newAddresses[i] = newAddresses[i].replace("[", "");
                newAddresses[i] = newAddresses[i].replace("]", "");
                newAddresses[i] = newAddresses[i].replace("\"", "");
                newAddresses[i] = newAddresses[i].replace(" ", "");
            }
            vo.setImageUrl(newAddresses);
            vo.setImageUrls(null);
        }
        PageInfo<ShowCollectProductsVO> pageInfo = new PageInfo<>(list1);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }
}
